home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / beos / PPBeDevKit.ZIP / PLAYERPR.TAR / PlayerPRO / Source / Import-Export / MADH.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-26  |  13.0 KB  |  495 lines

  1. /********************                        ***********************/
  2. //
  3. //    Player PRO 5.0 - DRIVER SOURCE CODE -
  4. //
  5. //    Library Version 5.0
  6. //
  7. //    To use with MAD Library for Mac: Symantec, CodeWarrior and MPW
  8. //
  9. //    Antoine ROSSET
  10. //    16 Tranchees
  11. //    1206 GENEVA
  12. //    SWITZERLAND
  13. //
  14. //    COPYRIGHT ANTOINE ROSSET 1996, 1997, 1998
  15. //
  16. //    Thank you for your interest in PlayerPRO !
  17. //
  18. //    FAX:                (+41 22) 346 11 97
  19. //    PHONE:             (+41 79) 203 74 62
  20. //    Internet:     RossetAntoine@bluewin.ch
  21. //
  22. /********************                        ***********************/
  23.  
  24. #include "MOD.h"
  25. #include "MAD.h"
  26. #include "MADH.h"
  27. #include "RDriver.h"
  28.  
  29. #if defined(powerc) || defined(__powerc)
  30. enum {
  31.         PlayerPROPlug = kCStackBased
  32.         | RESULT_SIZE(SIZE_CODE(sizeof(OSErr)))
  33.         | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof( OSType)))
  34.         | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof( Ptr)))
  35.         | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof( MADMusic*)))
  36.         | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof( PPInfoRec*)))
  37.         | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof( MADDriverSettings*)))
  38. };
  39.  
  40. ProcInfoType __procinfo = PlayerPROPlug;
  41. #else
  42. #include <A4Stuff.h>
  43. #endif
  44.  
  45. Ptr MADPlugNewPtr( long size, MADDriverSettings* init)
  46. {
  47.     if( init->sysMemory) return NewPtrSys( size);
  48.     else return NewPtr( size);
  49. }
  50.  
  51. Ptr MADPlugNewPtrClear( long size, MADDriverSettings* init)
  52. {
  53.     if( init->sysMemory) return NewPtrSysClear( size);
  54.     else return NewPtrClear( size);
  55. }
  56.  
  57. enum
  58. {
  59.     ins     = 1,
  60.     note    = 2,
  61.     cmd        = 4,
  62.     argu    = 8,
  63.     vol        = 16
  64. };
  65.  
  66. oldPatData* oldDecompressPartitionMAD1( oldMADSpec *header, oldPatData* myPat)
  67. {
  68.     oldPatData*                finalPtr;
  69.     Byte                     *srcPtr;
  70.     oldCmd                    *myCmd;
  71.     short                    maxCmd;
  72.     Byte                    set;
  73.     
  74.     finalPtr = ( oldPatData*) NewPtrClear( sizeof( oldPatHeader) + myPat->header.size * header->numChn * sizeof( oldCmd));
  75.     if( finalPtr == 0L) return 0L;
  76.     
  77.     BlockMoveData( myPat, finalPtr, sizeof( oldPatHeader));
  78.  
  79.     srcPtr = (Byte*) myPat->Cmds;
  80.     myCmd = (oldCmd*) finalPtr->Cmds;
  81.     maxCmd = finalPtr->header.size * header->numChn;
  82.     
  83.     /*** Decompression Routine ***/
  84.     
  85.     while( maxCmd != 0)
  86.     {
  87.         maxCmd--;
  88.         
  89.         myCmd->cmd     = 0;
  90.         myCmd->note = 0xFF;
  91.         myCmd->arg     = 0;
  92.         myCmd->ins     = 0;
  93.         myCmd->vol     = 0xFF;
  94.         
  95.         set = *srcPtr++;
  96.         
  97.         if( set & ins)    myCmd->ins = *srcPtr++;
  98.         if( set & note)    myCmd->note = *srcPtr++;
  99.         if( set & cmd)    myCmd->cmd = *srcPtr++;
  100.         if( set & argu)    myCmd->arg = *srcPtr++;
  101.         if( set & vol)    myCmd->vol = *srcPtr++;
  102.         
  103.         myCmd++;
  104.     }
  105.     
  106.     return finalPtr;
  107. }
  108.  
  109. oldCmd* GetOldCommand( short PosX, short    TrackIdX, oldPatData*    tempMusicPat)
  110. {
  111.     if( PosX < 0) PosX = 0;
  112.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  113.     
  114.     return( &(tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  115. }
  116.  
  117. Cmd* GetMADCommand( register short PosX, register short    TrackIdX, register PatData*    tempMusicPat)
  118. {
  119.     if( PosX < 0) PosX = 0;
  120.     else if( PosX >= tempMusicPat->header.size) PosX = tempMusicPat->header.size -1;
  121.         
  122.     return( & (tempMusicPat->Cmds[ (tempMusicPat->header.size * TrackIdX) + PosX]));
  123. }
  124.  
  125. void pStrcpy(register unsigned char *s1, register unsigned char *s2)
  126. {
  127.     register short len, i;
  128.     
  129.     len = *s2;
  130.     for ( i = 0; i <= len; i++) s1[ i] = s2[ i];
  131. }
  132.  
  133. void mystrcpy( Ptr a, Ptr b)
  134. {
  135.     BlockMove( b + 1, a, b[ 0]);
  136. }
  137.  
  138. OSErr MADH2Mad( Ptr MADPtr, long size, MADMusic *theMAD, MADDriverSettings *init)
  139. {
  140. short                     i, x;
  141. long                     inOutCount, OffSetToSample = 0L, z;
  142. OSErr                    theErr = noErr;
  143. Ptr                        tempPtr;
  144. long             finetune[16] = 
  145.                 {
  146.                     8363,    8413,    8463,    8529,    8581,    8651,    8723,    8757,
  147.                     7895,    7941,    7985,    8046,    8107,    8169,    8232,    8280
  148.                 };
  149.  
  150.  
  151. /**** Old MADH variables ****/
  152.  
  153. oldMADSpec                *oldMAD;
  154.  
  155. oldMAD = (oldMADSpec*) MADPtr;
  156.  
  157.  
  158. /**** HEADER ****/
  159. if( oldMAD->MAD != 'MADH') return MADFileNotSupportedByThisPlug;
  160. OffSetToSample += sizeof( oldMADSpec);
  161.  
  162. // Conversion
  163. inOutCount = sizeof( MADSpec);
  164. theMAD->header = (MADSpec*) MADPlugNewPtrClear( inOutCount, init);    
  165. if( theMAD->header == 0L) return MADNeedMemory;
  166.  
  167. theMAD->header->MAD = 'MADI';
  168.  
  169. BlockMove( oldMAD->name, theMAD->header->name, 32);
  170. theMAD->header->numPat            = oldMAD->numPat;
  171. theMAD->header->numChn            = oldMAD->numChn;
  172. theMAD->header->numPointers        = oldMAD->numPointers;
  173. BlockMove( oldMAD->oPointers, theMAD->header->oPointers, 256);
  174. theMAD->header->speed            = oldMAD->speed;
  175. theMAD->header->tempo            = oldMAD->tempo;
  176.  
  177. mystrcpy( theMAD->header->infos, (Ptr) "\pConverted by PlayerPRO MAD-F-G Plug (⌐Antoine ROSSET <rossetantoine@bluewin.ch>)");
  178.  
  179. /**** Patterns *******/
  180.  
  181. for( i = 0; i < oldMAD->numPat; i++)
  182. {
  183.     struct oldPatData        *tempPat, *tempPat2;
  184.     struct oldPatHeader        tempPatHeader;
  185.     
  186.     /** Lecture du header de la partition **/
  187.     inOutCount = sizeof( struct oldPatHeader);
  188.     BlockMoveData( MADPtr + OffSetToSample, &tempPatHeader, inOutCount);
  189.     
  190.     /*************************************************/
  191.     /** Lecture du header + contenu de la partition **/
  192.     /*************************************************/
  193.     
  194.     if( tempPatHeader.compMode == 'MAD1')
  195.     {
  196.         inOutCount = sizeof( oldPatData) + tempPatHeader.patBytes;
  197.     }
  198.     else
  199.     {
  200.         inOutCount = sizeof( oldPatData) + oldMAD->numChn * tempPatHeader.size * sizeof( oldCmd);
  201.     }
  202.     
  203.     tempPat = (struct oldPatData*) MADPlugNewPtr( inOutCount, init);
  204.     if( tempPat == 0L) DebugStr("\pMemory Prob1");
  205.     
  206.     BlockMove( MADPtr + OffSetToSample, tempPat, inOutCount);
  207.     OffSetToSample += inOutCount;
  208.     
  209.     if( tempPat->header.compMode == 'MAD1')
  210.     {
  211.         tempPat2 = oldDecompressPartitionMAD1( oldMAD, tempPat);
  212.         
  213.         DisposePtr( (Ptr) tempPat);
  214.         
  215.         tempPat = tempPat2;
  216.     }
  217.     
  218.     /**************/
  219.     /* CONVERSION */
  220.     /**************/
  221.     
  222.     theMAD->partition[ i] = (PatData*) MADPlugNewPtrClear( sizeof( PatHeader) + theMAD->header->numChn * tempPat->header.size * sizeof( Cmd), init);
  223.     if( theMAD->partition[ i] == 0L) return MADNeedMemory;
  224.     
  225.     theMAD->partition[ i]->header.size         = tempPat->header.size;
  226.     theMAD->partition[ i]->header.compMode     = 'NONE';
  227.     
  228.     BlockMove( tempPat->header.name, theMAD->partition[ i]->header.name, 20);
  229.     
  230.     theMAD->partition[ i]->header.patBytes = 0L;        theMAD->partition[ i]->header.unused2 = 0L;
  231.     
  232.     for( x = 0; x < theMAD->partition[ i]->header.size; x++)
  233.     {
  234.         for( z = 0; z < theMAD->header->numChn; z++)
  235.         {
  236.             struct oldCmd *oldCmd;
  237.             Cmd    *aCmd;
  238.             
  239.             aCmd = GetMADCommand(  x,  z, theMAD->partition[ i]);
  240.             
  241.             oldCmd     = GetOldCommand(    x,
  242.                                         z,
  243.                                         tempPat);
  244.             
  245.             aCmd->ins         = oldCmd->ins;
  246.             aCmd->note         = oldCmd->note;
  247.             aCmd->cmd         = oldCmd->cmd;
  248.             aCmd->arg         = oldCmd->arg;
  249.             aCmd->vol        = oldCmd->vol;
  250.             aCmd->unused     = oldCmd->unused;
  251.         }
  252.     }
  253.     
  254.     DisposePtr( (Ptr) tempPat);
  255.     tempPat = 0L;
  256. }
  257. for( i = theMAD->header->numPat; i < MAXPATTERN ; i++) theMAD->partition[ i] = 0L;
  258.  
  259. for( i = 0; i < MAXTRACK; i++)
  260. {
  261.     if( i % 2 == 0) theMAD->header->chanPan[ i] = MAX_PANNING/4;
  262.     else theMAD->header->chanPan[ i] = MAX_PANNING - MAX_PANNING/4;
  263.     
  264.     theMAD->header->chanVol[ i] = MAX_VOLUME;
  265. }
  266.  
  267.     theMAD->header->generalVol        = 64;
  268.     theMAD->header->generalSpeed    = 80;
  269.     theMAD->header->generalPitch    = 80;
  270.  
  271. /**** Instruments header *****/
  272.  
  273. theMAD->fid = ( InstrData*) MADPlugNewPtrClear( sizeof( InstrData) * (long) MAXINSTRU, init);
  274. if( !theMAD->fid) return MADNeedMemory;
  275.  
  276. theMAD->sample = ( sData**) MADPlugNewPtrClear( sizeof( sData*) * (long) MAXINSTRU * (long) MAXSAMPLE, init);
  277. if( !theMAD->sample) return MADNeedMemory;
  278.  
  279. for( i = 0; i < MAXINSTRU; i++) theMAD->fid[ i].firstSample = i * MAXSAMPLE;
  280.  
  281. for( i = 0; i < 64; i++)
  282. {
  283.     InstrData    *curIns = &theMAD->fid[ i];
  284.     
  285.     BlockMove( oldMAD->fid[ i].name, theMAD->fid[ i].name, 32);
  286.     
  287.     theMAD->fid[ i].type = oldMAD->fid[ i].type;
  288.     theMAD->fid[ i].numSamples = oldMAD->fid[ i].numSamples;
  289.     BlockMove( oldMAD->fid[ i].what, theMAD->fid[ i].what, 96);
  290.     BlockMove( oldMAD->fid[ i].volEnv, theMAD->fid[ i].volEnv, 12 * sizeof( EnvRec));
  291.     BlockMove( oldMAD->fid[ i].pannEnv, theMAD->fid[ i].pannEnv, 12 * sizeof( EnvRec));
  292.     theMAD->fid[ i].volSize = oldMAD->fid[ i].volSize;
  293.     theMAD->fid[ i].pannSize = oldMAD->fid[ i].pannSize;
  294.     theMAD->fid[ i].volSus = oldMAD->fid[ i].volSus;
  295.     theMAD->fid[ i].volBeg = oldMAD->fid[ i].volBeg;
  296.     theMAD->fid[ i].volEnd = oldMAD->fid[ i].volEnd;
  297.     theMAD->fid[ i].pannSus = oldMAD->fid[ i].pannSus;
  298.     theMAD->fid[ i].pannBeg = oldMAD->fid[ i].pannBeg;
  299.     theMAD->fid[ i].pannEnd = oldMAD->fid[ i].pannEnd;
  300.     theMAD->fid[ i].volType = oldMAD->fid[ i].volType;
  301.     theMAD->fid[ i].pannType = oldMAD->fid[ i].pannType;
  302.     theMAD->fid[ i].volFade = oldMAD->fid[ i].volFade;
  303.     theMAD->fid[ i].vibDepth = oldMAD->fid[ i].vibDepth;
  304.     theMAD->fid[ i].vibRate = oldMAD->fid[ i].vibRate;
  305.     
  306.     for( x = 0; x < oldMAD->fid[ i].numSamples ; x++)
  307.     {
  308.         oldsData     *oldcurData;
  309.         sData         *curData;
  310.         
  311.         oldcurData = (oldsData*) (MADPtr + OffSetToSample);
  312.         OffSetToSample += sizeof( oldsData);
  313.         
  314.         curData = theMAD->sample[ i*MAXSAMPLE + x] = (sData*) MADPlugNewPtrClear( sizeof( sData), init);
  315.         
  316.         curData->size        = oldcurData->size;
  317.         curData->loopBeg     = oldcurData->loopBeg;
  318.         curData->loopSize     = oldcurData->loopSize;
  319.         curData->vol        = oldcurData->vol;
  320.         curData->c2spd        = oldcurData->c2spd;
  321.         curData->loopType    = oldcurData->loopType;
  322.         curData->amp        = oldcurData->amp;
  323.     //    curData->panning    = oldcurData->panning;
  324.         curData->relNote    = oldcurData->relNote;
  325.         
  326.         curData->data         = MADPlugNewPtr( curData->size, init);
  327.         if( curData->data == 0L) return MADNeedMemory;
  328.             
  329.         BlockMove( MADPtr + OffSetToSample, curData->data, curData->size);
  330.         OffSetToSample += curData->size;
  331.     }
  332. }
  333.  
  334. return noErr;
  335. }
  336.  
  337. OSErr TestoldMADFile( Ptr AlienFile)
  338. {
  339. OSType    *myMADSign = (OSType*) AlienFile;
  340.  
  341. if(    *myMADSign == 'MADH') return   noErr;
  342. else return  MADFileNotSupportedByThisPlug;
  343. }
  344.  
  345. OSErr ExtractoldMADInfo( PPInfoRec *info, Ptr AlienFile)
  346. {
  347.     oldMADSpec    *myMOD = ( oldMADSpec*) AlienFile;
  348.     long        PatternSize;
  349.     short        i;
  350.     short        tracksNo;
  351.     
  352.     /*** Signature ***/
  353.     
  354.     info->signature = myMOD->MAD;
  355.     
  356.     /*** Internal name ***/
  357.     
  358.     myMOD->name[ 31] = '\0';
  359.     MADstrcpy( info->internalFileName, myMOD->name);
  360.     
  361.     /*** Tracks ***/
  362.     
  363.     info->totalTracks = myMOD->numChn;
  364.         
  365.     /*** Total Patterns ***/
  366.     
  367.     info->totalPatterns = 0;
  368.     for( i = 0; i < 128; i++)
  369.     {
  370.         if( myMOD->oPointers[ i] >= info->totalPatterns)    info->totalPatterns = myMOD->oPointers[ i];
  371.     }
  372.     info->totalPatterns++;
  373.     
  374.     /*** Partition Length ***/
  375.     
  376.     info->partitionLength = myMOD->numPointers;
  377.     
  378.     /*** Total Instruments ***/
  379.     
  380.     for( i = 0, info->totalInstruments = 0; i < 64 ; i++)
  381.     {
  382.         if( myMOD->fid[ i].numSamples > 0) info->totalInstruments++;
  383.     }
  384.     
  385.     MADstrcpy( info->formatDescription, "MADH Plug");
  386.  
  387.     return noErr;
  388. }
  389.  
  390. /*****************/
  391. /* MAIN FUNCTION */
  392. /*****************/
  393.  
  394. OSErr main( OSType order, char *AlienFileName, MADMusic *MadFile, PPInfoRec *info, MADDriverSettings *init)
  395. {
  396.     OSErr    myErr;
  397.     Ptr        AlienFile;
  398.     short    vRefNum, iFileRefI;
  399.     long    dirID, sndSize;
  400.     
  401. #ifndef powerc
  402.     long    oldA4 = SetCurrentA4();             //this call is necessary for strings in 68k code resources
  403. #endif
  404.  
  405.     c2pstr( AlienFileName);
  406.     
  407.     myErr = noErr;
  408.  
  409.     switch( order)
  410.     {
  411.         case 'IMPL':
  412.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  413.             if( myErr == noErr)
  414.             {
  415.                 GetEOF( iFileRefI, &sndSize);
  416.                 
  417.                 // ** MEMORY Test Start
  418.                 AlienFile = MADPlugNewPtr( sndSize * 2L, init);
  419.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  420.                 // ** MEMORY Test End
  421.                 
  422.                 else
  423.                 {
  424.                     DisposePtr( AlienFile);
  425.                     
  426.                     AlienFile = MADPlugNewPtr( sndSize, init);
  427.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  428.                     if( myErr == noErr)
  429.                     {
  430.                         myErr = TestoldMADFile( AlienFile);
  431.                         if( myErr == noErr)
  432.                         {
  433.                             myErr = MADH2Mad( AlienFile, GetPtrSize( AlienFile), MadFile, init);
  434.                         }
  435.                     }
  436.                     DisposePtr( AlienFile);    AlienFile = 0L;
  437.                 }
  438.                 FSClose( iFileRefI);
  439.             }
  440.         break;
  441.         
  442.         case 'TEST':
  443.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  444.             if( myErr == noErr)
  445.             {
  446.                 sndSize = 5000L;    // Read only 5000 first bytes for optimisation
  447.                 
  448.                 AlienFile = MADPlugNewPtr( sndSize, init);
  449.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  450.                 else
  451.                 {
  452.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  453.                     myErr = TestoldMADFile( AlienFile);
  454.                     
  455.                     DisposePtr( AlienFile);    AlienFile = 0L;
  456.                 }
  457.                 FSClose( iFileRefI);
  458.             }
  459.         break;
  460.  
  461.         case 'INFO':
  462.             myErr = FSOpen( (unsigned char*) AlienFileName, 0, &iFileRefI);
  463.             if( myErr == noErr)
  464.             {
  465.                 GetEOF( iFileRefI, &info->fileSize);
  466.             
  467.                 sndSize = 5000L;    // Read only 5000 first bytes for optimisation
  468.                 
  469.                 AlienFile = MADPlugNewPtr( sndSize, init);
  470.                 if( AlienFile == 0L) myErr = MADNeedMemory;
  471.                 else
  472.                 {
  473.                     myErr = FSRead( iFileRefI, &sndSize, AlienFile);
  474.                     if( myErr == noErr)
  475.                     {
  476.                         myErr = ExtractoldMADInfo( info, AlienFile);
  477.                     }
  478.                     DisposePtr( AlienFile);    AlienFile = 0L;
  479.                 }
  480.                 FSClose( iFileRefI);
  481.             }
  482.         break;
  483.         
  484.         default:
  485.             myErr = MADOrderNotImplemented;
  486.         break;
  487.     }
  488.  
  489.     p2cstr( (unsigned char*) AlienFileName);
  490.  
  491.     #ifndef powerc
  492.         SetA4( oldA4);
  493.     #endif
  494.     return myErr;
  495. }